using MediatR;
using SmartApplication.Api.Applient.Command.Feed;
using SmartApplication.ApiCode;
using SmartApplication.Domain.Entity;
using SmartApplication.Infrastructure.Instrument;

namespace SmartApplication.Api.Applient.CommandHandler.Feed
{
    public class DeleteHealthHandler : IRequestHandler<DeleteHealthCommand, ApiResult<int>>
    {
        public DeleteHealthHandler(IBaseRepository<HealthModel> healthRepository)
        {
            HealthRepository = healthRepository;
        }

        public IBaseRepository<HealthModel> HealthRepository { get; }

        /// <summary>
        /// 保健信息逻辑删除
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<ApiResult<int>> Handle(DeleteHealthCommand request, CancellationToken cancellationToken)
        {
            var res = new ApiResult<int>();

            var health = await HealthRepository.GetValueAsync(request.Id);

            if (health == null)
            {
                res.Code = EnumCode.Fail;
                res.Message = "没有查找到您所需要的数据";
                return res;
            }

            health.IsDeleted = true;

            var del = await HealthRepository.UpdateAsync(health);
            if (del > 0)
            {
                res.Code = EnumCode.Success;
                res.Message = "逻辑删除成功";
                res.Data = del;
                return res;
            }
            else
            {
                res.Code = EnumCode.Fail;
                res.Message = "逻辑删除失败";
                res.Data = del;
                return res;
            }
        }
    }
}